/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Lista.java
 *
 * Created on 22/04/2013, 05:57:54
 */
package gui;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import visual.ontogrator.pkg2.Gene;

/**
 *
 * @author Gabriel
 */
public class Lista extends javax.swing.JFrame {
     DefaultListModel listModel = new DefaultListModel();
     DefaultListModel listPesquisa = new DefaultListModel();
     DefaultListModel listRede = new DefaultListModel();
     ArrayList<Gene> agenerede = new ArrayList<>();

    public ArrayList<Gene> getAgenerede() {
            
        return agenerede;
    }
   
    public void setAgenerede(ArrayList<Gene> agenerede) {
        this.agenerede = agenerede;
      
       
    }
    
    

    public void copytoAgenerede(ArrayList<Gene> agenepesquisa) {
        boolean flag=false;
        ArrayList<Gene> aux = new ArrayList<>();
        for (int j = 0; j < agenepesquisa.size(); j++) {
            Gene genepesquisa = agenepesquisa.get(j);
            String pesquisa = genepesquisa.getNome_Lista();
            for (int i = 0; i < agenerede.size(); i++) {

                flag = false;
                Gene generede = agenerede.get(i);
                //verifica se o gene ja esta na lsita
                String rede = generede.getNome_Lista();
                if (rede.equals(pesquisa)) {
                    flag = true;
                    //remover gene antigo a adiciona o novo
                    agenerede.remove(i);
                    aux.add(genepesquisa); 
                    i=agenerede.size();
                }
            }
            //adiciona gene novo na lista
            if(!flag){
               agenerede.add(genepesquisa); 
            }
        }
        
        for (int i = 0; i < aux.size(); i++) {
            agenerede.add(aux.get(i));
        }
 
    }

     
     String txtlista;

    public String getTxtlista() {
        return jTextArea1.getText();
    }

    public void setTxtlista(String txtlista) {
        this.txtlista = txtlista;

        geraListModel(txtlista);

        lstGenes.setModel(listModel);
        lstGenes.repaint();

        
    }
    public DefaultListModel getListPesquisa() {
        return listPesquisa;
    }

    public void setListPesquisa(DefaultListModel listPesquisa) {
        this.listPesquisa = listPesquisa;
        lstPesquisa.setModel(listPesquisa);
        lstPesquisa.repaint();
    }

    public DefaultListModel getListModel() {
        return listModel;
    }

    public void setListModel(DefaultListModel listModel) {
        this.listModel = listModel;
        lstGenes.setModel(listModel);
        lstGenes.repaint();
    }
    /** Creates new form Lista */
    public Lista() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        btnIncluir = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        lstGenes = new javax.swing.JList();
        btnRemoveGene = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        btnTransfere = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        lstPesquisa = new javax.swing.JList();
        btnRemovePesquisa = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        chkYEAST = new javax.swing.JCheckBox();
        chkTAIR = new javax.swing.JCheckBox();
        chkKEGG = new javax.swing.JCheckBox();
        chkNCBI = new javax.swing.JCheckBox();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.LINE_AXIS));

        jPanel1.setPreferredSize(new java.awt.Dimension(900, 500));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        jPanel1.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 50, 131, 295));

        btnIncluir.setText("Transferir");
        btnIncluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIncluirActionPerformed(evt);
            }
        });
        jPanel1.add(btnIncluir, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 370, -1, -1));

        jLabel2.setText("Digite os genes a serem incluidos:");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, -1, -1));

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jScrollPane3.setViewportView(lstGenes);

        jPanel2.add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, 125, 295));

        btnRemoveGene.setText("Remover");
        btnRemoveGene.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveGeneActionPerformed(evt);
            }
        });
        jPanel2.add(btnRemoveGene, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 350, -1, -1));

        jLabel1.setText("Lista de genes:");
        jPanel2.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 10, -1, -1));

        btnTransfere.setText(">");
        btnTransfere.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTransfereActionPerformed(evt);
            }
        });
        jPanel2.add(btnTransfere, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 30, -1, -1));

        jLabel3.setText("Lista de genes para pesquisar:");
        jLabel3.setToolTipText("");
        jPanel2.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 10, -1, -1));

        jScrollPane4.setViewportView(lstPesquisa);

        jPanel2.add(jScrollPane4, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 30, 125, 295));

        btnRemovePesquisa.setText("Remover");
        btnRemovePesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemovePesquisaActionPerformed(evt);
            }
        });
        jPanel2.add(btnRemovePesquisa, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 350, -1, -1));

        jLabel5.setText("Remover itens selecionados");
        jPanel2.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 330, -1, -1));

        jLabel6.setText("Remover itens selecionados");
        jPanel2.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 330, -1, -1));

        jButton1.setText("Gerar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 260, -1, -1));
        jPanel2.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 80, 170, -1));

        jLabel8.setText("<html>\nGerar lista dos genes<br> com erro de pesquisa no:\n</html>");
        jPanel2.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 140, -1, -1));

        chkYEAST.setSelected(true);
        chkYEAST.setText("YEAST");
        jPanel2.add(chkYEAST, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 230, -1, -1));

        chkTAIR.setSelected(true);
        chkTAIR.setText("TAIR");
        jPanel2.add(chkTAIR, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 170, -1, -1));

        chkKEGG.setSelected(true);
        chkKEGG.setText("KEGG");
        jPanel2.add(chkKEGG, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 190, -1, -1));

        chkNCBI.setSelected(true);
        chkNCBI.setText("NCBI");
        chkNCBI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkNCBIActionPerformed(evt);
            }
        });
        jPanel2.add(chkNCBI, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 210, -1, -1));

        jPanel1.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 30, 500, 390));

        jLabel7.setText("Incluir na \"Lista de genes\"");
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 350, -1, -1));

        getContentPane().add(jPanel1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnRemoveGeneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveGeneActionPerformed
    int[] selecionados = lstGenes.getSelectedIndices();


       int size = selecionados.length-1;  
                  
                for(int i=size;i>=0;i--)  
                {  
                   listModel.remove(selecionados[i]);  
                }  
    lstGenes.setModel(listModel);
    lstGenes.repaint();
              
}//GEN-LAST:event_btnRemoveGeneActionPerformed

private DefaultListModel geraListModel(String txt){
    DefaultListModel list = new DefaultListModel();
    String str[] = txt.split("\n");
      for (String nlista : str) {
                if(!listModel.contains(nlista)){ 
                listModel.addElement(nlista);
                }
           }
    
    return list;
}
private void btnIncluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIncluirActionPerformed

           geraListModel(jTextArea1.getText());
           jTextArea1.setText("");
           jTextArea1.repaint();
           lstGenes.setModel(listModel);
           lstGenes.repaint();
}//GEN-LAST:event_btnIncluirActionPerformed

private void btnRemovePesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemovePesquisaActionPerformed
   int[] selecionados = lstPesquisa.getSelectedIndices();


       int size = selecionados.length-1;  
                  
                for(int i=size;i>=0;i--)  
                {  
                    
                   listPesquisa.remove(selecionados[i]);  
                }  
    lstPesquisa.setModel(listPesquisa);
    lstPesquisa.repaint();
}//GEN-LAST:event_btnRemovePesquisaActionPerformed

public void transfereLista(){
    listPesquisa = listModel;

    lstPesquisa.setModel(listPesquisa);
    lstPesquisa.repaint();
}
private void btnTransfereActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTransfereActionPerformed
 int[] selecionados = lstGenes.getSelectedIndices();

    for (int i = 0; i < selecionados.length; i++) {


        if (!listPesquisa.contains(listModel.get(selecionados[i]))) {
            listPesquisa.addElement(listModel.get(selecionados[i]));

        }

    }
    lstPesquisa.setModel(listPesquisa);
    lstPesquisa.repaint();
    
}//GEN-LAST:event_btnTransfereActionPerformed
private DefaultListModel unirList(DefaultListModel list1, DefaultListModel list2){
    DefaultListModel listunida=list1;
    
    for (int i = 0; i < list2.size(); i++) {
         if (!listunida.contains(list2.get(i))) {
             listunida.addElement(list2.get(i));
         }
      
    }
    
    return listunida;
}
private DefaultListModel geraListBancos(String parametro){
    //foi usado reflection para testar se os genes estão com erros ou nao e gerar o list model para cada banco
    Class cls = Gene.class;
    Method validate = null;
    boolean erro=true;
    
    try {
       validate = cls.getMethod(parametro);
       
    } catch (Exception e) {
        System.out.println("Erro ao capturar metodo de validação do banco");
        System.out.println(e);
    }
    DefaultListModel list = new DefaultListModel();
     for (int i = 0; i < agenerede.size(); i++) {
            
         try {
                erro = (boolean)validate.invoke(agenerede.get(i));
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (IllegalArgumentException ex) {
                ex.printStackTrace();
            } catch (InvocationTargetException ex) {
                ex.printStackTrace();
            }

         if (!erro) {
            list.addElement(agenerede.get(i).getNome_Lista());
        }
    }
     return list;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

    listPesquisa.removeAllElements();

    String validar = "isValidar";
    DefaultListModel listTAIR = new DefaultListModel();
    DefaultListModel listKEGG = new DefaultListModel();
    DefaultListModel listNCBI = new DefaultListModel();
    DefaultListModel listYEAST = new DefaultListModel();
   
    if (chkTAIR.isSelected()) {
        listTAIR = geraListBancos(validar + "TAIR");
    }

    if (chkKEGG.isSelected()) {
       listKEGG = geraListBancos(validar + "KEGG");
    }
    if (chkNCBI.isSelected()) {
        listNCBI = geraListBancos(validar + "NCBI");
    }
    if (chkYEAST.isSelected()) {
        listYEAST = geraListBancos(validar + "YEAST");
    }
    
    listPesquisa = unirList(listTAIR,unirList(listKEGG,unirList(listNCBI,listYEAST)));
    
    lstPesquisa.setModel(listPesquisa);
    lstPesquisa.repaint();
}//GEN-LAST:event_jButton1ActionPerformed

private void chkNCBIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkNCBIActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_chkNCBIActionPerformed
private void geraListaRede(){
    listRede.clear();
    for (int i = 0; i < agenerede.size(); i++) {
                listRede.addElement(agenerede.get(i).getNome_Lista());
              
    }
}
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Lista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Lista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Lista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Lista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Lista().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnIncluir;
    private javax.swing.JButton btnRemoveGene;
    private javax.swing.JButton btnRemovePesquisa;
    private javax.swing.JButton btnTransfere;
    private javax.swing.JCheckBox chkKEGG;
    private javax.swing.JCheckBox chkNCBI;
    private javax.swing.JCheckBox chkTAIR;
    private javax.swing.JCheckBox chkYEAST;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JList lstGenes;
    private javax.swing.JList lstPesquisa;
    // End of variables declaration//GEN-END:variables
}
